package com.huang.app.antstore.wxapi;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.huang.app.antstore.common.R;
import com.huang.app.antstore.common.utils.Constants;
import com.huang.app.antstore.common.utils.SPUtils;
import com.huang.app.antstore.common.utils.SweetAlertDialogFactory;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import cn.pedant.SweetAlert.SweetAlertDialog;

public class WXPayEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);

        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @SuppressLint("StringFormatInvalid")
    @Override
    public void onResp(BaseResp resp) {
        //		Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            final int payType = (int) SPUtils.get(getContext(), Constants.WEIXIN_PAY_TYPE, 0);
            if (resp.errCode == 0) {
                SweetAlertDialogFactory.build(this, SweetAlertDialog.SUCCESS_TYPE, false).setContentText("支付成功").setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        sweetAlertDialog.dismiss();
                        Intent intent = new Intent();
                        switch (payType) {
                            case Constants.WEIXIN_PAY_GOODS_ORDER:
//                                intent.setClass(getContext(), GoodsOrderActivity.class);
//                                intent.putExtra(Constants.KEY_OBJ1, 1);//跳转到订单列表的已支付标签页
//                                startActivity(intent);
                                break;
                            case Constants.WEIXIN_PAY_RECHARGE://到余额页面
//                                intent.setClass(getContext(), BalanceActivity.class);
//                                intent.putExtra(Constants.KEY_OBJ1, 3);//只可以为任意值 只要有这个key就认为是支付成功了
//                                startActivity(intent);
                                break;
                            case Constants.WEIXIN_PAY_MASTER_CERTIFICATE://到首页我的页面
//                                intent.setClass(getContext(), MainActivity.class);
//                                intent.putExtra(Constants.KEY_OBJ1, 3);
//                                startActivity(intent);
                                break;
                        }
                        finish();
                    }
                }).show();

            } else if (resp.errCode == -1) {
                SweetAlertDialogFactory.build(this, SweetAlertDialog.ERROR_TYPE, false).setContentText("支付失败").setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        sweetAlertDialog.dismiss();
                        Intent intent = new Intent();
                        switch (payType) {
                            case Constants.WEIXIN_PAY_GOODS_ORDER:
//                                intent.setClass(getContext(), GoodsOrderActivity.class);
//                                intent.putExtra(Constants.KEY_OBJ1, 0);//跳转到订单列表的未支付标签页
//                                startActivity(intent);
                                break;
                            case Constants.WEIXIN_PAY_RECHARGE://不用任何处理 直接finish返回 重新充值或者取消
                                break;
                            case Constants.WEIXIN_PAY_MASTER_CERTIFICATE://不用任何处理 直接finish返回 重新认证.或者取消
                                break;
                        }
                        finish();
                    }
                }).show();
            } else {//-2 用户取消了支付
                SweetAlertDialogFactory.build(this, SweetAlertDialog.ERROR_TYPE, false).setContentText("支付已取消").setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        sweetAlertDialog.dismiss();
                        Intent intent = new Intent();
                        switch (payType) {
                            case Constants.WEIXIN_PAY_GOODS_ORDER:
//                                intent.setClass(getContext(), GoodsOrderActivity.class);
//                                intent.putExtra(Constants.KEY_OBJ1, 0);//跳转到订单列表的未支付标签页
//                                startActivity(intent);
                                break;
                            case Constants.WEIXIN_PAY_RECHARGE://不用任何处理 直接finish返回 重新充值或者取消
                                break;
                            case Constants.WEIXIN_PAY_MASTER_CERTIFICATE://不用任何处理 直接finish返回 重新认证.或者取消
                                break;
                        }
                        finish();
                    }

                }).show();
            }

        }
    }

    private Context getContext() {
        return this;
    }
}